#include "TestWidget.h"
#include "ui_TestWidget.h"

TestWidget::TestWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TestWidget)
{
    ui->setupUi(this);

    // 树初始化
    treeInit();

    connect(ui->btn_treeTest, SIGNAL(clicked()),this, SLOT(slotClicked()));
    slotClicked();
}

TestWidget::~TestWidget()
{
    if(nullptr == ui)
    {
        delete ui;
        ui = nullptr;
    }
}

void TestWidget::slotItemClicked(QTreeWidgetItem *, int )
{

}

void TestWidget::slotItemDoubleClicked(QTreeWidgetItem *, int )
{

}

void TestWidget::slotShowMenu(QPoint point)
{
    qout "Menu pos " << point;
    QTreeWidgetItem *item = m_tree->itemAt(point);
    if(nullptr != item)
    {
        qout item->text(0);
    }
}

// 鼠标悬浮事件
void TestWidget::slotMouseSuspension(QModelIndex index)
{
    QRect rect= m_tree->visualRect(index);
    printMsg(QPoint(rect.x(),rect.y()));

    // 原理
    this->setWindowFlags(Qt::FramelessWindowHint);  // 去除标题栏
    QPoint point = QCursor::pos();                  // 屏幕相对于父窗口的距离
    QPoint p0 = QPoint(m_tree->frameGeometry().x(), m_tree->frameGeometry().y());
    QPoint p1 = QPoint(m_tree->geometry().x(), m_tree->geometry().y());
    QPoint p2 = QPoint(this->frameGeometry().x(), this->frameGeometry().y());
    QPoint p3 = QPoint(this->geometry().x(), this->geometry().y());
    QPoint target = point - p3;
    printMsg(target);
}

// tree测试
void TestWidget::slotClicked()
{
    if(nullptr == m_tree)
    {
        return ;
    }

    for(int i=0; i<2; ++i)
    {
        QTreeWidgetItem *topItem = new QTreeWidgetItem();
        QString topIndex = QString("自动滚动测试") + QString::number(i);
        topItem->setText(0, topIndex);
        m_tree->addTopLevelItem(topItem);

        for(int j=0; j<10; ++j)
        {
            QTreeWidgetItem *item = new QTreeWidgetItem();
            QString itemIndex = QString("自动滚动测试") + QString::number(i) + QString::number(j);
            item->setText(0, itemIndex);
            topItem->addChild(item);
        }
    }
}

// 树初始化
void TestWidget::treeInit()
{
    m_tree = new QTreeWidget(this);
    // 鼠标悬浮事件
    m_tree->setMouseTracking(true);
    connect(m_tree, SIGNAL(entered(QModelIndex)), this, SLOT(slotMouseSuspension(QModelIndex)));

    m_tree->setObjectName("SmoothScrollTree");
    m_tree->header()->hide();
    m_tree->setContextMenuPolicy(Qt::CustomContextMenu);  // 菜单策略： 右键菜单
    // 滚动条
    m_tree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_tree->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // 形状
    m_tree->setColumnWidth(0, 300);       // 列宽
    m_tree->setGeometry(80, 100, 300, 400);  // 位置

    // 信号
    connect(m_tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(slotItemClicked(QTreeWidgetItem*,int)));
    connect(m_tree, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(slotItemDoubleClicked(QTreeWidgetItem*,int)));
    connect(m_tree, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotShowMenu(QPoint)));
}

// 输出节点信息
void TestWidget::printMsg(QPoint point)
{
    qout point;
    QTreeWidgetItem *item = m_tree->itemAt(point);
    if(nullptr != item)
    {
        qout item->text(0);
    }
}
